package com.atl.service.impl;

import com.atl.enity.po.AgentUser;
import com.atl.enity.po.ChargingStations;
import com.atl.enity.web.dto.AgentUserSearchDTO;
import com.atl.enity.web.dto.ChargingStationsSearchDTO;
import com.atl.execption.BusinessException;
import com.atl.mapper.ChargingStationsMapper;
import com.atl.service.IChargingStationsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 充电站 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@Service
public class ChargingStationsServiceImpl extends ServiceImpl<ChargingStationsMapper, ChargingStations> implements IChargingStationsService {


    /**
     *
     * 查询
     *
     * @param chargingStationsSearchDTO
     * @return
     */
    @Override
    public Page<ChargingStations> search(ChargingStationsSearchDTO chargingStationsSearchDTO) {
        QueryWrapper<ChargingStations> queryWrapper = new QueryWrapper<>();

        String stationName = chargingStationsSearchDTO.getStationName();
        Integer isDisable = chargingStationsSearchDTO.getIsDisable();
        String stationType = chargingStationsSearchDTO.getStatioType();
        String owningArrier = chargingStationsSearchDTO.getOwningArrier();
        String approvalStatus = chargingStationsSearchDTO.getApprovalStatus();
        String operationType = chargingStationsSearchDTO.getOperationType();

        if (StringUtils.isBlank(operationType)) {
            throw new BusinessException(-222, "请选择运行类型");
        }
        //添加运营类型
        queryWrapper.eq("operation_type", operationType);

        if (StringUtils.isNotBlank(stationName)) {
            queryWrapper.like("station_name", stationName);
        }

        String startTime = chargingStationsSearchDTO.getStartTime();
        String endTime = chargingStationsSearchDTO.getEndTime();
        if (startTime != null && endTime != null) {
            LocalDateTime date1 = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            LocalDateTime date2 = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atTime(LocalTime.MAX);
            queryWrapper.between("timestamp", date1, date2);
        }

        if (isDisable != null) {
            queryWrapper.eq("is_disable", isDisable);
        }
        if (StringUtils.isNotBlank(stationType)) {
            queryWrapper.eq("statio_type", stationType);
        }
        if (StringUtils.isNotBlank(owningArrier)) {
            queryWrapper.eq("owning_arrier", owningArrier);
        }
        if (StringUtils.isNotBlank(approvalStatus)) {
            queryWrapper.eq("approval_status", approvalStatus);
        }

        return this.page(new Page<>(chargingStationsSearchDTO.getCurrent(), chargingStationsSearchDTO.getPageSize()), queryWrapper);
    }

}
